#include <cstdio>
#include <cstring>

const int N = 3e3 + 5, inf = 0x3f3f3f3f;

int n, p;
int a[N], sum[N], f[N][N], k[N][N];

inline int calc(int x, int y) {
  int mid = (x + y) >> 1;
  return sum[y] - sum[mid] - (y - mid) * a[mid] + (mid - x) * a[mid] - sum[mid - 1] +
         sum[x - 1];
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("P4767 [IOI2000]邮局.in", "r", stdin);
  freopen("P4767 [IOI2000]邮局.out", "w", stdout);
#endif
#endif

  scanf("%d%d", &n, &p);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    sum[i] = sum[i - 1] + a[i];
  }
  memset(f, inf, sizeof(f));
  for (int i = 1; i <= n; ++i) f[i][1] = calc(1, i);
  for (int i = 2; i <= p; ++i) {
    for (int j = n; j >= i; --j) {
      k[n + 1][i] = n;
      for (int h = k[j][i - 1], tmp; h <= k[j + 1][i]; ++h) {
        tmp = f[h][i - 1] + calc(h + 1, j);
        if (tmp < f[j][i]) {
          f[j][i] = tmp;
          k[j][i] = h;
        }
      }
    }
  }
  printf("%d", f[n][p]);
  return 0;
}